package com.zhb.lkd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhb.lkd.base.PageResponse;
import com.zhb.lkd.enums.HttpsCode;
import com.zhb.lkd.exception.BusinessException;
import com.zhb.lkd.mapper.RoleMapper;
import com.zhb.lkd.pojos.dto.LoginDto;
import com.zhb.lkd.pojos.entity.Role;
import com.zhb.lkd.pojos.entity.User;
import com.zhb.lkd.mapper.UserMapper;
import com.zhb.lkd.pojos.vo.LoginVo;
import com.zhb.lkd.pojos.vo.UserVo;
import com.zhb.lkd.properties.JwtProperties;
import com.zhb.lkd.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhb.lkd.utils.JwtUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author haibin
 * @since 2024-08-18
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Resource
    private JwtProperties jwtProperties;
    @Resource
    private RoleMapper roleMapper;

    /**
     * 登录
     *
     * @param loginDto
     * @return
     */
    @Override
    public LoginVo login(LoginDto loginDto) {
        //1.检查参数
        if (StrUtil.isBlank(loginDto.getLoginName())) {
            throw new BusinessException(500, "请输入账户");
        }
        if (StrUtil.isBlank(loginDto.getPassword())) {
            throw new BusinessException(500, "请输入密码");
        }
        //2.判断登录账号密码
        User user = lambdaQuery().eq(User::getLoginName, loginDto.getLoginName()).one();
        if (user == null) {
            throw new BusinessException(HttpsCode.USET_NOT);
        }
        //判断密码
        if (!user.getLoginName().equals(loginDto.getLoginName()) && !user.getPassword().equals(loginDto.getPassword())) {
            throw new BusinessException(HttpsCode.PASSWORD_ERROR);
        }
        //3.封装返回数据
        //生成token
        String token = JwtUtils.createJwt(user.getId(), jwtProperties.getTime(), jwtProperties.getKey());
        LoginVo loginVo = BeanUtil.copyProperties(user, LoginVo.class);
        loginVo.setUserId(user.getId());
        loginVo.setToken(token);
        return loginVo;
    }

    /**
     * 用户管理
     * @param pageIndex
     * @param pageSize
     * @param userName
     * @return
     */
    @Override
    public PageResponse search(Integer pageIndex, Integer pageSize, String userName) {
        //1.参数检查
        if (pageIndex == null || pageSize == null) {
            throw new BusinessException(500, "参数错误");
        }
        //2.分页查询
        Page<User> page = lambdaQuery()
                .like(StrUtil.isNotBlank(userName), User::getUserName, userName)
                .page(new Page<>(pageIndex, pageSize));
        //拼接权限数据
        List<UserVo> userVos = page.getRecords().stream().map(new Function<User, UserVo>() {
            @Override
            public UserVo apply(User user) {
                UserVo userVo = BeanUtil.copyProperties(user, UserVo.class);
                //根据权限id查询权限表
                Role role = roleMapper.selectById(user.getRoleId());
                userVo.setRole(role);
                return userVo;
            }
        }).collect(Collectors.toList());
        //3.返回数据
        return new PageResponse(page,userVos);
    }

    /**
     * 新增人员
     * @param user
     */
    @Override
    public void create(User user) {
        //1.检查参数
        if (StrUtil.isBlank(user.getUserName())){
            throw new BusinessException(500,"登录名不能为空");
        }
        //2.保存数据
        try {
            save(user);
        }catch (Exception e){
            throw new BusinessException(500,"保存失败,请检查用户名是否重复");
        }
    }

    /**
     * 删除人员
     * @param userId
     */
    @Override
    public void deleteById(String userId) {
        //1.检查参数
        if (StrUtil.isBlank(userId)){
            throw new BusinessException(500,"id不能为空");
        }
        //2.删除
        removeById(userId);
    }

    /**
     * 修改人员
     * @param user
     */
    @Override
    public void updateByUserId(User user) {
        //1.检查参数
        if (user.getId()==0||user.getId()==null){
            throw new BusinessException(500,"id不能为空");
        }
        //2.修改
        updateById(user);
    }
}
